package com.company.controller;

import com.company.common.R;
import com.company.entity.Yaopinfenlei;
import com.company.service.YaopinfenleiService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class YaopinfenleiController {
    @Resource
    YaopinfenleiService yaopinfenleiService;

    /*
    * 药品分类 分页查询
    * */
    @CrossOrigin(origins = "*")
    @RequestMapping("/selectAllYaoPinFenLei/{page}")
    public R selectAllYaoPinFenLei(@PathVariable("page") int page){
        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,4);
        List<Yaopinfenlei> list=this.yaopinfenleiService.list();
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }

    /*
    * 药品分类根据id删除
    * */
    @CrossOrigin(origins = "*")
    @RequestMapping("/deleteMenu2/{id}")
    public R del(@PathVariable("id") int id){
        boolean remove = this.yaopinfenleiService.removeById(id);
        R r=new R();
        if(remove==true){
            r.getMessage();
        }else{
            r.getMessage();
        }
        return r;
    }
    /*
    * 药品分类添加
    * */
    @CrossOrigin(origins = "*")
    @PostMapping("/insetyaopinfenlei")
    public R inset(@RequestBody Yaopinfenlei yaopinfenlei){
        boolean b = this.yaopinfenleiService.save(yaopinfenlei);
        R r = new R();
        return r.add("b",b);
    }

    /*
    * 药品分类根据id查询
    * */
    @CrossOrigin(origins = "*")
    @PostMapping("/selectbyid3")
    public R selectbyid(@RequestBody Yaopinfenlei yaopinfenlei){
        Yaopinfenlei byid = this.yaopinfenleiService.getById(yaopinfenlei.getId());
        return new R().add("byid",byid);
    }

    /*
    * 药品分类根据id修改
    * */
    @CrossOrigin(origins = "*")
    @PostMapping("/update3")
    public R uodate(@RequestBody Yaopinfenlei yaopinfenlei){
        this.yaopinfenleiService.updateById(yaopinfenlei);
        return new R();
    }
}
